package linestrategy

import (
	"codejy/analysis/lineanalysis"
	"codejy/globalfunc"
	"codejy/store"
	"codejy/structdef"
	"strings"
)

// 类型行为调用策略 (不支持多线程)
type GlobalPropAssignmentStrategy struct {
}

// 注册策略
func init() {
	RegisterStrategy(&GlobalPropAssignmentStrategy{})
}

// 判断
func (f *GlobalPropAssignmentStrategy) getId() int {
	return lineanalysis.GlobalPropAssignment
}

// 调用此方法必然在实例内部方法中,所以CurrentInstanceAddress不会为空
func (f *GlobalPropAssignmentStrategy) Execute(threadStore *structdef.ThreadStore) {
	content := threadStore.Code.LineContent
	//获取=号前面的变量名称
	index := strings.Index(content, "=")
	variableName := content[:index]
	//获取=号后面的内容
	expression := content[index+1:]
	//截取.号后面的属性名称
	index1 := strings.Index(variableName, ".")
	propName := variableName[index1+1:] //属性名称
	//计算表达式
	calculateExpression1 := globalfunc.ExecuteFunc("AssignmentStrategyMode", threadStore, expression).(string)
	//获取值类型
	variableType := globalfunc.ExecuteFunc("CodeAnalysis", threadStore, calculateExpression1).(int)
	//修改全局变量
	globalenv := threadStore.GlobalEnv
	store.ModifyVariableScope(threadStore, globalenv, structdef.NewVarDefinitionValue(propName, calculateExpression1, variableType))
}
